﻿using System;
using System.Collections.Generic;
using T.FAS.Runtime.Base.Common;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Expression.Service;
using T.FAS.Runtime.ScheduleJob.Engine;
using T.FAS.Runtime.ScheduleJob.Manager;
using T.FAS.Runtime.ScheduleJob.Service;
using T.FAS.Runtime.ScheduleJob.SGSPI;
using T.FAS.Runtime.ScheduleJob.SPI;
using T.ServiceComponent.Kernel.Service;

namespace T.FAS.Runtime.ScheduleJob.SGPlugin
{
    [SGService]
    public class ScheduleJobSubscriptionSGService : IScheduleJobSubscriptionSGService
    {
        [SGService(ServiceID = "FASR-CreateScheduleJobSubscription")]

        public string CreateScheduleJobSubscription(ScheduleJobSubscription subscription)
        {
            var service = ScheduleJobSubscriptionService.GetInstance();
            var subId = service.CreateScheduleJobSubscription(subscription);
            return subId;
        }

        public List<ScheduleJobSubscription> GetScheduleJobSubscriptionsByFilter(UserDefineQueryFilter filter)
        {
            var service = ScheduleJobSubscriptionManager.GetInstance();
            var result = service.GetScheduleJobSubscriptionsByFilter(filter);
            return result;
        }

        public ScheduleJobSubscription GetScheduleJobSubscriptionById(string subscriptionID)
        {
            var service = ScheduleJobSubscriptionManager.GetInstance();
            var subscription = service.GetScheduleJobSubscriptionById(subscriptionID);
            if(subscription?.NoticeSets != null)
            {
                foreach (var item in subscription.NoticeSets)
                {
                    //var param = JsonConvert.DeserializeObject<Dictionary<string, object>>(subscription.JSContentParams);
                    //dynamic result = ExprService.GetInstance.JSExecute(subscription.JSContent, param);
                    //if (result?.Result != null)
                    //{
                    //    item.NoticeContent = item.NoticeContent.Replace("${DataCount}", result.Result.Count.ToString());
                    //}
                    item.NoticeContent = item.NoticeContent.Replace("${DataCount}", "N");
                }
            }
            return subscription;
        }

        public void CancleScheduleJobSubscription(string subscriptionID)
        {
            var service = ScheduleJobSubscriptionService.GetInstance();
            service.CancleScheduleJobSubscription(subscriptionID);
        }

        public List<SysExecuteFrequency> GetSysExecuteFrequencies()
        {
            var service = SysExecuteFrequencyService.GetInstance();
            return service.GetSysExecuteFrequencies();
        }
        public SubscriptionNoticeSetView GetNoticeScopeSets()
        {
            var service = SubscriptionNoticeScopeService.GetInstance();
            return service.GetNoticeScopeSets();
        }
    }
}
